﻿using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal class OfficeArtFSP : OfficeArtRecord
    {
        private bool _fBackground;
        private bool _fChild;
        private bool _fConnector;
        private bool _fDeleted;
        private bool _fFlipH;
        private bool _fFlipV;
        private bool _fGroup;
        private bool _fHaveAnchor;
        private bool _fHaveMaster;
        private bool _fOleShape;
        private bool _fPatriarch;
        private uint _spid;
        private MSOSPT? _spt;

        public MSOSPT? ShapeType
        {
            get
            {
                return _spt;
            }
            set
            {
                _spt = value;
            }
        }

        public OfficeArtFSP(uint spid)
        {
            _spid = spid;
        }

        public OfficeArtFSP()
        {
        }

        public override RecType RecordType
        {
            get { return RecType.OfficeArtFSP; }
        }


        protected uint Al
        {
            get
            {
                uint result = 0;
                if (FGroup) result |= 0x0001;
                if (_fChild) result |= 0x0002;
                if (FPatriarch) result |= 0x0004;
                if (_fDeleted) result |= 0x0008;
                if (_fOleShape) result |= 0x00010;
                if (_fHaveMaster) result |= 0x0020;
                if (_fFlipH) result |= 0x0040;
                if (_fFlipV) result |= 0x0080;
                if (_fConnector) result |= 0x0100;
                if (FHaveAnchor) result |= 0x0200;
                if (_fBackground) result |= 0x0400;
                if (_spt != null) result |= 0x0800;
                return result;
            }
        }

        protected override byte Version
        {
            get { return 0x2; }
        }

        protected override int ByteLength
        {
            get { return 8; }
        }

        public uint SpId
        {
            get { return _spid; }
        }

        public bool FPatriarch
        {
            get { return _fPatriarch; }
            set { _fPatriarch = value; }
        }

        public bool FGroup
        {
            get { return _fGroup; }
            set { _fGroup = value; }
        }

        public bool FHaveAnchor
        {
            get { return _fHaveAnchor; }
            set { _fHaveAnchor = value; }
        }


        protected override void ReadNoHeader(OfficeArtReadContext context)
        {
            _spid = context.ReadUInt32();
            uint al = context.ReadUInt32();
            FGroup = (al & 0x0001) != 0;
            _fChild = (al & 0x0002) != 0;
            FPatriarch = (al & 0x0004) != 0;
            _fDeleted = (al & 0x0008) != 0;
            _fOleShape = (al & 0x00010) != 0;
            _fHaveMaster = (al & 0x0020) != 0;
            _fFlipH = (al & 0x0040) != 0;
            _fFlipV = (al & 0x0080) != 0;
            _fConnector = (al & 0x0100) != 0;
            FHaveAnchor = (al & 0x0200) != 0;
            _fBackground = (al & 0x0400) != 0;
            var fHaveSpt = (al & 0x0800) != 0;
            ShapeType = fHaveSpt ? (MSOSPT?) rhrecInstance : null;
            Contract.Assert(Al == al);
        }


        protected override void WriteNoHeader(OfficeArtWriteContext context)
        {
            context.Write(_spid);
            context.Write(Al);
        }
        protected override void PrepareHeader(ref OfficeArtRecordHeader rh)
        {
            if (this.ShapeType != null)
                rh.recInstance = (ushort) ShapeType.Value;
            base.PrepareHeader(ref rh);
        }
#if DEBUG
        public void CheckCompatible(OfficeArtClientAnchorSheet clientAnchor, OfficeArtClientTextbox clientTextbox, OfficeArtFOPT shapes)
        {
            Contract.Assert((clientAnchor != null) == (this.FHaveAnchor));
            Contract.Assert((shapes != null) == (this.ShapeType != null));
        }
#endif
    }
}